package cn.felord.core.config;

import cn.felord.core.shiro.tag.ShiroTags;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.IOException;

/**
 * Created by Administrator on 2016/3/15.
 */
@Configuration
public class ShiroTagFreeMarkerConfigurer {

    /**
     * Free marker configurer free marker configurer.
     *
     * @return the free marker configurer
     * @throws IOException       the io exception
     * @throws TemplateException the template exception
     */
    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPath("/templates/");

        freemarker.template.Configuration configuration = configurer.createConfiguration();
        configuration.setTemplateUpdateDelayMilliseconds(0);
        configuration.setClassicCompatible(true);
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        configuration.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
        configuration.setNumberFormat("0.##########");
        configuration.setSharedVariable("shiro", new ShiroTags());
        configurer.setConfiguration(configuration);
        return configurer;
    }

}
